<?php
require_once 'header.php';

require_once '../include/pager.php';

switch($clause)
{
	case "addinfo":
		menu();
		addinfo();
		break;
	case "saveinfo":
		saveinfo();
		break;
	case "editinfo":
		menu();
		editinfo();
		break;
	case "saveeditinfo":
		saveeditinfo();
		break;
	case "unlockinfo":
		unlockinfo();
		break;
	case "lockinfo":
		lockinfo();
		break;
	case "unvipinfo":
		unvipinfo();
		break;
	case "vipinfo":
		vipinfo();
		break;
	case "delinfo":
		delinfo();
		break;
	case "addcode":
		addcode();
		break;
	default:
		menu();
		main();
		break;
}






function main()
{
	global $db,$LoginEdUserName;

	global $stype,$keyword;
	$where = "";

	$linkUrl =  '&page=';

	global $page;
	$list_num = 15;
	$total_count = $db->counter("`h_code`", "1 = 1 ", 'code_id');$page = (int)$page;$rowset = new Pager($total_count,$list_num,$page);$from_record = $rowset->_offset();$page_list = $rowset->link($linkUrl);
	$query = "select a.*,b.h_userName from `h_code` as a LEFT JOIN  h_member as b  on a.member_id = b.id  order by a.code_id desc LIMIT $from_record, $list_num";
	$result = $db->query($query);
	//$query = "Select * from `h_point2_shop` order by h_addTime desc";
	//$result = $db->query($query);
	$rs_list = array();
	while($list = $db->fetch_array($result))
	{
		$rs_list[]=$list;
	}

	foreach($rs_list as $key =>$value){
		if($value['is_used'] == 0){
			$value['is_usedtext'] = '否';
		}else{
			$value['is_usedtext'] = '是';
		}
		$rs_list[$key] = $value;
	}

	?>
	<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="tableborder">
		<tr>
			<td height="25" colspan="11" align="center" class="tdtitle">一级会员邀请码管理</td>
		</tr>
		<tr align="center">
			<td width="20%" height="23" class="tdtitle-title">邀请码</td>
			<td class="tdtitle-title">一级会员</td>
			<td class="tdtitle-title">是否使用</td>
			<td class="tdtitle-title">相关操作</td>
		</tr>
		<?php
		foreach ($rs_list as $key=>$val)
		{
			?>
			<tr align="center" class="tdbottom" onMouseOver="javascript:this.className='tdbottomover';" onMouseOut="javascript:this.className='tdbottom';">
				<td align="center"><?php echo $val['code']; ?><br /></td>
				<td align="center"><?php echo $val['h_userName']; ?><br /></td>
				<td align="center"><?php echo $val['is_usedtext']; ?><br /></td>
				<td>
					<?php if($val['is_used'] == 0){ ?>
					<a style="cursor:pointer;" onClick="javascript:hintandturn('确定要删除吗？数据将不可恢复！','?clause=delinfo&id=<?php echo $val['code_id']; ?>',true);">删除</a>
					<?php }?>
				</td>
			</tr>
			<?php
		}
		?>
	</table>
	<?php
	if(count($rs_list) > 0) echo "<div style='text-align:center;'>$page_list</div>";
}



function menu()
{
	global $stype,$keyword;
	if(strlen($stype) <= 0){
		$stype = -1;
	}
	?>
	<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="tableborder">
		<tr>
			<td height="25" class="tdtitle" align="center">相关操作</td>
		</tr>
		<tr>
			<td height="23" class="tdbottom" align="center"><a href="#" onClick="javascript:hintandturn('确定生成邀请码！','?clause=addcode',true);">添加邀请码</a> | <a href="?">邀请码管理</a></td>
		</tr>

	</table>
	<br />
	<?php
}



function addcode()
{

	global $db,$id;



	do{
		$code = '';

		for ($j = 1; $j <= 8; $j++) {
			$code .= rand(0,9);
		}

		$rs = $db->get_one("select * from `h_code` where code = '$code' ");

	}while($rs);

	$sql = "insert into `h_code` set ";
	$sql .= "member_id =  0, ";
	$sql .= "code = '" . $code . "', ";
	$sql .= "is_used = 0 ";

	$db->query($sql);

	turnToPage('?');
}


function delinfo()
{
	global $db,$id;

	$query = "delete from `h_point2_shop` where id = $id";
	$db->query($query);

	turnToPage('?');
}


footer();
?>